import * as types from '../action-typess';
import { getList, getDetail, getComment, postComment, addLike, postTopic, getOssToken, uploadPic,getApplyJob } from '../../api/community';

export function fetchTopicList() {
  return function(dispatch,getState) {
    dispatch({type: types.FETCH_TOPICLIST});
    let {page} = getState().community;
    getList(page).then(payload=>{
      dispatch({type: types.FETCH_TOPICLIST_FULFILLED, payload: payload});
    })
  }
}


export function fetchApplyJobList() {
  return function(dispatch,getState) {
    dispatch({type: types.FETCH_APPLY_JOB});
    let {page} = getState().community.applyJob;
    getApplyJob(page).then(payload=>{
      dispatch({type: types.FETCH_APPLY_JOB_FULFILLED, payload: payload});
    })
  }
}

export function submitTopic(param) {
  return function(dispatch) {
    dispatch({type: types.SUBMIT_TOPIC});
    postTopic(param).then(payload=>{
      dispatch({type: types.SUBMIT_TOPIC_FULFILLED, payload: payload});
    }, err=>{
      if(err.response.data.ret == -1){
        alert(err.response.data.msg);
      }
      dispatch({type: types.SUBMIT_TOPIC_FULFILLED, payload:err.response.data });
    })
  }
}

export function getTopicDetail(id) {
  return function(dispatch) {
    dispatch({type: types.TOPIC_DETAIL});
    getDetail(id).then(payload=>{
      dispatch({type: types.TOPIC_DETAIL_FULFILLED, payload: payload});
    })
  }
}

export function getOss() {
  return function(dispatch) {
    dispatch({type: types.OSS_TOKEN});
    getOssToken().then(payload=>{
      dispatch({type: types.OSS_TOKEN_FULFILLED, payload: payload});
    })
  }
}

export function submitComment(params) {
  return function(dispatch) {
    dispatch({type: types.SUBMIT_COMMENT});
    postComment(params).then(payload=>{
      dispatch({type: types.SUBMIT_COMMENT_FULFILLED, payload: payload});
    })
  }
}

export function likeComment(cid) {
  return function(dispatch) {
    dispatch({type: types.LIKE_COMMENT});
    addLike(cid).then(payload=>{
      dispatch({type: types.LIKE_COMMENT_FULFILLED, payload: payload});
    })
  }
}

export function uploadImg(param) {
  return function(dispatch) {
    dispatch({type: types.UPLOAD_IMG});
    uploadPic(param).then(payload=>{
      dispatch({type: types.UPLOAD_IMG_FULFILLED, payload: payload});
    })
  }
}
